<?php
/**
 * 微信订单退款
 */
namespace Org\Util;
class WxRefund{
	public $appid;//appid
	public $mch_id;//商户id
	public $transaction_id;//微信订单号
	public $out_trade_no;//商户订单 号
	public $out_refund_no;//退款订单号
	public $total_fee;//订单支付总金额
	public $refund_fee;//退款金额
	public $op_user_id;//操作员帐号, 默认为商户号
	public $key;//支付密匙
	public $post_url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';//接口地址
 	public function __construct($appid,$mch_id,$transaction_id,$out_trade_no,$out_refund_no,$total_fee,$refund_fee,$op_user_id,$key) {
        $this->appid = $appid;
        $this->mch_id = $mch_id;
        $this->transaction_id = $transaction_id;
        $this->out_trade_no = $out_trade_no;
        $this->out_refund_no =$out_refund_no;
        $this->total_fee =$total_fee;
        // $this->total_fee =1;
        $this->refund_fee =$refund_fee;
        $this->op_user_id =$op_user_id;
        $this->key=$key;
    }
    /**
     * 
     * 生成提交参数
     */
    public function setParam(){
    	$param['appid'] = $this->appid;
    	$param['mch_id'] = $this->mch_id;
    	$param['nonce_str'] = $this->createNoncestr();
    	if (!empty($this->transaction_id)){
    		$param['transaction_id'] = $this->transaction_id;
    	}else{
    		$param['out_trade_no'] = $this->out_trade_no;
    	}
    	$param['out_refund_no'] = $this->out_refund_no;
    	$param['total_fee'] = $this->total_fee;
    	$param['refund_fee'] = $this->refund_fee;
    	$param['op_user_id'] = $this->op_user_id;
    	$param['mch_id'] = $this->mch_id;
    	$param['mch_id'] = $this->mch_id;
    	$param['sign'] = $this->create_sign($param);
    	$xml = $this->arrayToXml($param);
    	return $xml;
    }
    /**
     * 
     * POST提交数据
     */
    public function ssl_post($xml){
    	$ch = curl_init();
    	curl_setopt($ch,CURLOPT_URL,$this->post_url);
    	curl_setopt($ch,CURLOPT_HEADER,0);
    	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    	curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    	curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
    	curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/apiclient_cert.pem');
    	curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
	    curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/apiclient_key.pem');
	    curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
	    curl_setopt($ch,CURLOPT_CAINFO,getcwd().'/rootca.pem');
	    curl_setopt($ch,CURLOPT_POST, 1);
	    curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
	    $data = curl_exec($ch);
   	 	$data =  $this->xmlToArray($data);
		if ($data['result_code'] =='SUCCESS'){
			$r_data['status'] = 1;
			$r_data['refund_id'] = $data['refund_id'];
			$r_data['refund_channel'] = $data['refund_channel'];//ORIGINAL—原路退款 BALANCE—退回到余额
			return $r_data;
		}else{
			$r_data['status'] = 0;
			$r_data['error'] = $data['return_msg'];
			return $r_data;
		}
    }
	/**
	 * 
	 * 生成认证签名
	 * create_time 2015-10-12
 	 * author wj   
	 */
	public function create_sign($data){
		$str ='';
		ksort($data);//数组排序
		foreach($data as $k=>$v){//拼接成相关规则的字符串
			if (!empty($v)){
				if ($str==''){
					$str =$k . "=" . $v ;
				}else{
					$str .='&'.$k . "=" . $v ;
				}
			}
		}
		$str .="&key=$this->key";
		//加密
		$str = MD5($str);
		$res = strtoupper($str);
		return $res;
	}
	/**
	 * 	作用：产生随机字符串，不长于32位
	 */
	public function createNoncestr( $length = 32 ){
		$chars = "abcdefghijklmnopqrstuvwxyz0123456789";  
		$str ="";
		for ( $i = 0; $i < $length; $i++ )  {  
			$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);  
		}  
		return $str;
	}
	/**
	 * 	作用：array转xml
	 */
	function arrayToXml($arr)
    {
        $xml = "<xml>";
        foreach ($arr as $key=>$val)
        {
        	 if (1==1)
        	 {
        	 	$xml.="<".$key.">".$val."</".$key.">"; 

        	 }
        	 else
        	 	$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";  
        }
        $xml.="</xml>";
        return $xml; 
    }
	/**
	 * 	作用：将xml转为array
	 */
	public function xmlToArray($xml)
	{		
        //将XML转为array        
        $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
		return $array_data;
	}
}
?>